<template>
  <el-dialog v-model="dialogFormVisible" :title="title" @close="closeDialog">
    <el-main>
      <div>
        <div style="padding-bottom: 20px; padding-top: 10px">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item @click="goMain()">全部文件</el-breadcrumb-item>

            <el-breadcrumb-item v-for="(item, index) in items"
              ><a @click="getData(item.id, index)">{{
                item.name
              }}</a></el-breadcrumb-item
            >
          </el-breadcrumb>
        </div>
        <el-table
          :data="list"
          style="width: 100%"
          @row-dblclick="clickRow"
          :row-class-name="tableRowClassName"
        >
          <el-table-column prop="name" label="文件夹名称">
            <template #default="scope">
              <div style="display: flex">
                <el-image :src="icon" style="width: 40px"></el-image>
                <span style="line-height: 40px; margin-left: 10px">{{
                  scope.row.name
                }}</span>
              </div>
            </template>
          </el-table-column>

          <template v-slot:empty>
            <el-empty :description="des"></el-empty>
          </template>
        </el-table>
      </div>
    </el-main>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="clear()">取消</el-button>
        <el-button type="primary" @click="save()">{{ title }}</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script>
import addEditPanFoldersView from "@/components/pan/addEditPanFolders.vue";
import addEditPanFilesView from "@/components/pan/addPanFileRightView.vue";

import addFileView from "@/components/addExcelFile.vue";
import { ElConfigProvider, ElMessageBox, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { Picture as IconPicture, Upload } from "@element-plus/icons-vue";

export default {
  name: "PanFoldersListView",
  data() {
    return {
      type: 0,
      title: "",
      page: 0,
      flag: 0,
      dialogFormVisible: false,
      pageSize: 10,
      id: 0,
      total: 0,
      limit: 10,
      row: {},
      list: [],
      icon: require("@/assets/folder.png"),
      form: {
        folderName: "",
        teacherNumber: "",
      },
      parentFolderId: 0,
      items: [],
      des: "",
    };
  },
  components: {
    addEditPanFoldersView,
    addEditPanFilesView,
    addFileView,
    IconPicture,
    ElConfigProvider,
    ElMessageBox,
    ElMessage,
    Upload,
  },
  setup() {
    return {
      locale: zhCn,
    };
  },

  methods: {
    clickRow(row, l, e) {
      console.log(l);
      console.log(e);
      const isObjectIncluded = this.items.includes(row);
      if (!isObjectIncluded) {
        if (this.level == 1) {
          this.items = [];
          this.items.push(row);
          this.level = this.level + 1;
        } else {
          this.items.push(row);
          this.level = this.level + 1;
        }
      }
      this.parentFolderId = row.id;
      this.getPanFoldersList();
    },
    getData(folderId, index) {
      this.parentFolderId = folderId;
      this.items.splice(index + 1); // Remove the last element
      this.getPanFoldersList();
    },
    goMain() {
      this.items = [];
      this.parentFolderId = 0;
      this.getPanFoldersList();
    },
    searchEnter: function (e) {
      // 使用 which 和 keyCode 属性来解决兼容问题
      var keyCode = window.event ? e.keyCode : e.which;
      var val = e.target.value;
      if (keyCode == 13 && val) {
        this.getPanFoldersList();
      }
    },
    save() {
      console.log(this.parentFolderId);
      console.log(this.id);
      console.log(this.row);
      console.log(this.flag);
      if (this.flag == 1) {
        if (this.row.parentFolderId>=0) {
          httpService(
            "/javaApi/panFolders/copy",
            "id=" + this.row.id + "&parentFolderId=" + this.parentFolderId,
            "post"
          ).then((data) => {
            if (data.code == 200) {
              ElMessage.success(data.message);
              this.$parent.getPanFilesList();
              this.clear();
            } else {
              ElMessage.error(data.message);
            }
          });
        } else {
          httpService(
            "/javaApi/panFiles/copy",
            "id=" + this.row.id + "&folderId=" + this.parentFolderId,
            "post"
          ).then((data) => {
            if (data.code == 200) {
              ElMessage.success(data.message);
              this.$parent.getPanFilesList();
              this.clear();
            } else {
              ElMessage.error(data.message);
            }
          });
      
        }
      } else {
        if (this.row.parentFolderId>=0) {
          httpService(
            "/javaApi/panFolders/edit",
            "id=" + this.row.id + "&parentFolderId=" + this.parentFolderId,
            "post"
          ).then((data) => {
            if (data.code == 200) {
              ElMessage.success(data.message);
              this.$parent.getPanFilesList();
              this.clear();
            } else {
              ElMessage.error(data.message);
            }
          });
        } else {
          httpService(
            "/javaApi/panFiles/edit",
            "id=" + this.row.id + "&folderId=" + this.parentFolderId,
            "post"
          ).then((data) => {
            if (data.code == 200) {
              ElMessage.success(data.message);
              this.$parent.getPanFilesList();
              this.clear();
            } else {
              ElMessage.error(data.message);
            }
          });
         
        }
      }
    },

    closeDialog() {
      this.clear();
    },
    clear() {
      (this.type = 0),
        (this.title = ""),
        (this.page = 0),
        (this.flag = 0),
        (this.dialogFormVisible = false),
        (this.pageSize = 10),
        (this.id = 0),
        (this.total = 0),
        (this.limit = 10),
        (this.row = {}),
        (this.list = []),
        (this.icon = require("@/assets/folder.png")),
        (this.parentFolderId = 0),
        (this.items = []),
        (this.des = "");
    },
    add() {
      this.$refs.addEditPanFilesView.title = "添加文件夹表";
      this.$refs.addEditPanFilesView.dialogFormVisible = true;
    },
    editPanFolders(obj) {
      this.$refs.addEditPanFoldersView.title = "修改信息";
      this.$refs.addEditPanFoldersView.getPanFoldersById(obj.id);
      this.$refs.addEditPanFoldersView.dialogFormVisible = true;
    },
    deletePanFolders(id) {
      ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          httpService(
            "/javaApi/panFolders/deleteById",
            "id=" + id,
            "post"
          ).then((data) => {
            if (data.code == 200) {
              ElMessage.success(data.message);
              this.getPanFoldersList();
            } else {
              ElMessage.error(data.message);
            }
          });
        })
        .catch(() => {});
    },
    reloadData() {
      this.page = 1;
      this.getPanFoldersList();
    },
    importExcel() {
      this.$refs.addFileView.dialogFormVisible = true;
      this.$refs.addFileView.title = "批量导入文件夹表";
      this.$refs.addFileView.uploadApiUrl = "/javaApi/panFolders/importExcel";
    },
    exportExcel() {
      httpService(
        "/javaApi/panFolders/exportExcel",
        {
          folderName: this.form.folderName,
          teacherNumber: this.form.teacherNumber,
        },
        "get"
      ).then((data) => {
        if (data.code == 200) {
          window.open(data.message, "_blank");
        }
      });
    },
    onSubmit() {
      this.getPanFoldersList();
    },
    // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
    changePageSize(size) {
      this.pageSize = size;
      this.limit = size;
      this.getPanFoldersList();
    },

    // 改变页码，page：回调参数，表示当前选中的“页码”
    changeCurrentPage(page) {
      this.page = page;
      this.getPanFoldersList();
    },
    tableRowClassName({ row, rowIndex }) {
      return "";
    },
    getPanFoldersList() {
      if (this.flag == 1) {
        this.des = "复制到此";
      } else {
        this.des = "移动到此";
      }
      httpService(
        "/javaApi/panFolders/list/" + this.page + "/" + this.pageSize,
        {
          folderName: this.form.folderName,
          parentFolderId: this.parentFolderId,
          isEffect: 0,
          teacherNumber: this.form.teacherNumber,
        },
        "get"
      ).then((data) => {
        this.list = data.object.list;
        this.total = data.object.total;
        this.pageCount = data.object.pages;
      });
    },
  },
};
</script>
<style scoped>
.el-main {
  padding: 0px;
  text-align: left;
}

.teacher-content {
  width: 100%;
}

.app-table {
  background: #eff3f5;
  padding: 10px;
}

.app-separator {
  padding-top: 10px;
  padding-bottom: 20px;
  padding-left: 10px;
}

.app-body {
  background: white;
  padding: 20px 20px 10px 20px;
  margin-bottom: 10px;
  min-height: calc(100vh - 204px);
}

.el-table .warning-row {
  --el-table-tr-bg-color: var(--el-color-warning-lighter);
}

.el-table .success-row {
  --el-table-tr-bg-color: var(--el-color-success-lighter);
}

.el-pagination {
  text-align: center;
  margin-top: 10px;
}
</style>

